<?php
/**
 * Created by PhpStorm.
 * User: itboot
 * Date: 18-9-19
 * Time: 下午3:56
 */
namespace Biz\model;
use Doraemon\pockets\datebase\BaseDbModel;

class DefaultAuthority extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop_info';

    const _DATABASE_ = 'shop_info';

    const _TABLE_NAME = 'default_authority';

    const STATUS_NO = 0;  //每一条数据的存在状态(0为不存在)

    const STATUS_YES = 1;  //每一条数据的存在状态(1为不存在)

    const SHOW_STATUS_YES = 1; //每一条数据的展示状态(1为不存在)

    const AUTHORITY_STATUS_TRUE = 1; //权限的状态（１为开启，０为关闭，２为默认)

    public static function  getAuthorityList($column = "*")
    {
        return self::select($column, [
            'AND'   => [
                'status' => self::STATUS_YES
            ],
            'ORDER' => ['sort' => 'DESC']
            ]);
    }

    public static function getShopAuthorityList($column = "*")
    {
        return self::select($column,[
            'AND'   => [
                'status' => self::STATUS_YES,
                'show_status' => self::SHOW_STATUS_YES
            ],
            'ORDER' => ['sort' => 'DESC']
        ]);
    }

    public static function getAuthorityRow(array $where, $column = "*")
    {
        return self::get($column, $where);
    }

    public static function validateSave($authority_name, $authority_key)
    {
        $byName =  self::get("*", ['status' => self::STATUS_YES, 'authority_name' => $authority_name]);
        $byKey = self::get("*", ['status' => self::STATUS_YES, 'authority_key' => $authority_key]);

        return ['name' => $byName, 'key' =>$byKey];
    }

    public static function addAuthorityInfo(array $authority_info)
    {
        self::insert([
            'authority_name' => $authority_info['authority_name'],
            'authority_key' => $authority_info['authority_key'],
            'authority_status' => self::AUTHORITY_STATUS_TRUE,
            'show_status' => self::SHOW_STATUS_YES,
            'status' => self::STATUS_YES
        ]);

        return self::id();
    }

    public static function saveAuthority(array $data, $authority_id)
    {
        return self::update($data, ['id' => $authority_id]);
    }

    public static function delAuthorityRow($authority_id)
    {
        if (empty($authority_id)) {
            return false;
        }

        return self::update(['status' => self::STATUS_NO], ['id' => $authority_id]);
    }

    public static function updateSort($place, $authority_id)
    {
        return self::update(['sort' => $place], ['id' => $authority_id]);
    }
}

